@echo off
title Tencent Weiyun Installer

:: Check network connectivity first
echo Checking network connectivity...
ping -n 1 dldir1v6.qq.com >nul 2>&1
if errorlevel 1 (
    echo Network connectivity issue detected! Please check your internet connection.
    echo Press any key to retry...
    pause
    exit /b 1
)

:: Clean up any existing temp directory first
echo Cleaning up previous temporary files...
rd /s /q "%temp%\weiyun_setup" 2>nul

:: Create temp directory
md "%temp%\weiyun_setup" 2>nul
cd /d "%temp%\weiyun_setup"

:: Download aria2c (using China mirror) with retry mechanism
echo Preparing download tool...
set max_retries=3
set retry_count=0

:download_aria2
set /a retry_count+=1
powershell -Command "& {$ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri 'https://gitee.com/xavierlab/aria2c/releases/download/1.37.0/aria2-1.37.0-win-64bit-build1.zip' -OutFile 'aria2.zip'}"
if not exist "aria2.zip" (
    if %retry_count% leq %max_retries% (
        echo Attempt %retry_count% of %max_retries% failed. Retrying...
        timeout /t 5
        goto download_aria2
    ) else (
        echo Failed to download aria2c after %max_retries% attempts!
        echo Please check your network connection and try again.
        pause
        exit /b 1
    )
)

powershell -Command "& {Expand-Archive -Path 'aria2.zip' -DestinationPath '.' -Force}"

:: Check if aria2c exists
if not exist "aria2-1.37.0-win-64bit-build1\aria2c.exe" (
    echo Failed to extract aria2c!
    echo Please try running the script again.
    pause
    exit /b 1
)

:: Download with aria2c multi-thread with improved parameters
echo Downloading Weiyun installer...
set retry_count=0

:download_weiyun
set /a retry_count+=1
aria2-1.37.0-win-64bit-build1\aria2c.exe --max-connection-per-server=16 --split=8 --min-split-size=1M --dir=. --out=WeiyunSetup.exe --max-overall-download-limit=0 --max-download-limit=0 --file-allocation=none --summary-interval=1 --retry-wait=10 --max-tries=10 --timeout=60 --connect-timeout=60 --async-dns=true --continue=true --allow-overwrite=true --auto-file-renaming=false "https://dldir1v6.qq.com/weiyun/electron-update/release/5.2.1505/WeiyunApp-Setup-X64-5.2.1505.exe"

:: Check download
if not exist "WeiyunSetup.exe" (
    if %retry_count% leq %max_retries% (
        echo Attempt %retry_count% of %max_retries% failed. Retrying Weiyun download...
        timeout /t 10
        goto download_weiyun
    ) else (
        echo Download failed after %max_retries% attempts!
        echo Please check your network connection and try again.
        pause
        exit /b 1
    )
)

:: Verify file size (minimum expected size 50MB)
for %%I in (WeiyunSetup.exe) do set size=%%~zI
if %size% LSS 52428800 (
    echo Warning: Downloaded file seems incomplete!
    echo File size: %size% bytes
    echo Please run the script again.
    del /f /q WeiyunSetup.exe
    pause
    exit /b 1
)

echo Installing...
start /wait WeiyunSetup.exe /S
if errorlevel 1 (
    echo Installation failed!
    echo Please try running the script again.
    pause
    exit /b 1
)

echo Cleaning temporary files...
cd /d "%temp%"
rd /s /q "%temp%\weiyun_setup"

echo Installation completed!

:: Start Weiyun (using cmd /c start to create a completely detached process)
echo Starting Weiyun...
cmd /c start /b "" "C:\Users\%USERNAME%\AppData\Local\Programs\WeiyunApp\WeiyunApp.exe"

timeout /t 3

:: Delete this script
echo Deleting installer script...
del /f /q "%~f0"
exit 